package com.netease.nim.uikit.business.session.viewholder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.MyWebViewClient;
import com.cuotibao.zhuoxiao.basics.utils.ShowImageJsWebInterface;
import com.netease.nim.uikit.R;

/**
 * @author hdx on 2019/2/27.
 */
public class ZuiWenDialog extends Dialog implements View.OnClickListener {
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    WebView webView;
    String mUri;

    public ZuiWenDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public ZuiWenDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public ZuiWenDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    protected ZuiWenDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public ZuiWenDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public void setUri(String uri) {
        mUri = uri;
    }


    public ZuiWenDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public ZuiWenDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zuiwen_dialog);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.content);
//        webView.setWebViewClient(new MyWebViewClient());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(false);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setCursiveFontFamily("cursive");
        webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
        webSettings.setLoadWithOverviewMode(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            webSettings.setMediaPlaybackRequiresUserGesture(true);
        }
        webSettings.setNeedInitialFocus(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            webSettings.setOffscreenPreRaster(true);
        }
        webSettings.setSaveFormData(true);
        webSettings.setSupportMultipleWindows(true);
        webView.addJavascriptInterface(new ShowImageJsWebInterface((Activity) mContext), "imageControl");
        webView.setWebViewClient(new MyWebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                imgReset();
            }
        });
        webView.loadUrl(mUri);
        titleTxt = findViewById(R.id.title);
        cancelTxt = findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);
        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setText(negativeName);
        }
    }

    /**
     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
     **/
    private void imgReset() {
        webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                "}" +
                "})()");
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cancel) {
            if (listener != null) {
                listener.onClick(this, false);
            }
            this.dismiss();
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }

//    private class MyWebViewClient extends WebViewClient {
//
//
//        @Override
//        public void onPageFinished(WebView view, String url) {
//            super.onPageFinished(view, url);
//            imgReset();//重置webview中img标签的图片大小
//            // html加载完成之后，添加监听图片的点击js函数
//            addImageClickListner();
//        }
//
//        @Override
//        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
//        }
//    }
//
//    /**
//     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
//     **/
//    private void imgReset() {
//        webView.loadUrl("javascript:(function(){" +
//                "var objs = document.getElementsByTagName('img'); " +
//                "for(var i=0;i<objs.length;i++)  " +
//                "{"
//                + "var img = objs[i];   " +
//                "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
//                "}" +
//                "})()");
//    }
//
//    private void addImageClickListner() {
//        // 这段js函数的功能就是，遍历所有的img节点，并添加onclick函数，函数的功能是在图片点击的时候调用本地java接口并传递url过去
//        webView.loadUrl("javascript:(function(){" +
//                "var objs = document.getElementsByTagName(\"img\"); " +
//                "for(var i=0;i<objs.length;i++)  " +
//                "{"
//                + "    objs[i].onclick=function()  " +
//                "    {  "
//                + "        window.imagelistner.openImage(this.src);  " +
//                "    }  " +
//                "}" +
//                "})()");
//    }
//
//    public static class JavaScriptInterface {
//
//        private Context context;
//
//        public JavaScriptInterface(Context context) {
//            this.context = context;
//        }
//
//        //点击图片回调方法
//        //必须添加注解,否则无法响应
//        @JavascriptInterface
//        public void openImage(String img) {
//            Log.i("TAG", "响应点击事件!");
//            Intent intent = new Intent();
//            intent.putExtra("uri", img);
//            intent.setClass(context, ZoomPicAcitivity.class);//BigImageActivity查看大图的类，自己定义就好
//            context.startActivity(intent);
//        }
//    }
}
